<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img0.baidu.com/it/u=1947349600,600376990&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >Go指针</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、指针"><a href="#一、指针" class="headerlink" title="一、指针"></a>一、指针</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 指针类型定义 <span class="keyword">var</span> 变量名 *类型	</span><br><span class="line">   <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> a *<span class="keyword">int</span></span><br><span class="line">        <span class="keyword">var</span> b <span class="keyword">int</span> = <span class="number">100</span></span><br><span class="line">        a = &amp;b</span><br><span class="line">        fmt.Printf(<span class="string">&quot;Type of %T\n&quot;</span>, a) <span class="comment">// Type of *int 可以看到类型为指针类型</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 指针类型变量的默认值为<span class="literal">nil</span>，也就是空地址</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> a *<span class="keyword">int</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;value of a %v\n&quot;</span>, a)</span><br><span class="line">        &#125; <span class="comment">// value of a &lt;nil&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 如何操作指针变量指向的地址里面的值</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> a *<span class="keyword">int</span></span><br><span class="line">            <span class="keyword">var</span> b <span class="keyword">int</span> = <span class="number">100</span></span><br><span class="line">            a = &amp;b</span><br><span class="line">            fmt.Printf(<span class="string">&quot;value of  %v\n&quot;</span>, *a)</span><br><span class="line">        &#125; <span class="comment">// 通过*符号可以获取指针变量指向的变量</span></span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> 通过指针修改变量的值</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">testPointer</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> a *<span class="keyword">int</span></span><br><span class="line">            <span class="keyword">var</span> b <span class="keyword">int</span> = <span class="number">100</span></span><br><span class="line">            a = &amp;b</span><br><span class="line">            *a = <span class="number">200</span></span><br><span class="line">            fmt.Println(*a)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            testPointer()</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="number">5.</span> 指针变量传参</span><br><span class="line">    <span class="number">5.1</span> 值传参</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">testPointer</span><span class="params">(a <span class="keyword">int</span>)</span></span> &#123;</span><br><span class="line">            a = <span class="number">1001</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">testPointer1</span><span class="params">(a *<span class="keyword">int</span>)</span></span> &#123;</span><br><span class="line">            *a = <span class="number">200</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> b <span class="keyword">int</span> = <span class="number">100</span></span><br><span class="line">            testPointer(b)</span><br><span class="line">            fmt.Printf(<span class="string">&quot;value of b: %v\n&quot;</span>, b)</span><br><span class="line">            testPointer1(&amp;b)</span><br><span class="line">            fmt.Printf(<span class="string">&quot;value of b: %v\n&quot;</span>, b)</span><br><span class="line">        &#125;  <span class="comment">// value of b: 100  value of b: 200</span></span><br><span class="line"></span><br><span class="line">	<span class="number">5.2</span> 指针传参</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">testPointer</span><span class="params">(a <span class="keyword">int</span>)</span></span> &#123;</span><br><span class="line">            fmt.Printf(<span class="string">&quot;2.addr of a: %p, value of a: %v\n&quot;</span>, &amp;a, a)</span><br><span class="line">            a = <span class="number">1001</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;3.addr of a: %p, value of a: %v\n&quot;</span>, &amp;a, a)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">testPointer1</span><span class="params">(a *<span class="keyword">int</span>)</span></span> &#123;</span><br><span class="line">            fmt.Printf(<span class="string">&quot;5.addr of a: %p, value of a: %v\n&quot;</span>, &amp;a, a)</span><br><span class="line">            *a = <span class="number">200</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;6.addr of a: %p, value of a: %v\n&quot;</span>, &amp;a, *a)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> b <span class="keyword">int</span> = <span class="number">100</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;1.addr of b: %p, value of b: %v\n&quot;</span>, &amp;b, b)</span><br><span class="line">            testPointer(b)</span><br><span class="line">            <span class="keyword">var</span> p *<span class="keyword">int</span> = &amp;b</span><br><span class="line">            fmt.Printf(<span class="string">&quot;4.addr of p: %p, value of p: %v\n&quot;</span>, &amp;p, p)</span><br><span class="line">            testPointer1(p)</span><br><span class="line">            fmt.Printf(<span class="string">&quot;value of b: %v\n&quot;</span>, b)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">		1.addr of b: 0xc000014088, value of b: 100</span></span><br><span class="line"><span class="comment">		2.addr of a: 0xc0000140c0, value of a: 100</span></span><br><span class="line"><span class="comment">		3.addr of a: 0xc0000140c0, value of a: 1001</span></span><br><span class="line"><span class="comment">		4.addr of p: 0xc000006030, value of p: 0xc000014088</span></span><br><span class="line"><span class="comment">		5.addr of a: 0xc000006038, value of a: 0xc000014088</span></span><br><span class="line"><span class="comment">		6.addr of a: 0xc000006038, value of a: 200</span></span><br><span class="line"><span class="comment">		从这可以看出，传值给函数，函数只是拷贝，然而只是值相同，内存地址并不会是相同的</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">	<span class="number">5.3</span> 数组传参</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">modify</span><span class="params">(arr *[3]<span class="keyword">int</span>)</span></span> &#123;</span><br><span class="line">            fmt.Printf(<span class="string">&quot;2:addr of arr: %p, value of arr: %v\n&quot;</span>, &amp;arr, arr)</span><br><span class="line">            (*arr)[<span class="number">0</span>] = <span class="number">90</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;3:addr of arr: %p, value of arr: %v\n&quot;</span>, &amp;arr, *arr)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            a := [<span class="number">3</span>]<span class="keyword">int</span>&#123;<span class="number">89</span>, <span class="number">90</span>, <span class="number">91</span>&#125;</span><br><span class="line">            fmt.Printf(<span class="string">&quot;1:addr of arr: %p, value of arr: %v\n&quot;</span>, &amp;a, a)</span><br><span class="line">            modify(&amp;a)</span><br><span class="line">            fmt.Printf(<span class="string">&quot;4:addr of arr: %p, value of arr: %v\n&quot;</span>, &amp;a, a)</span><br><span class="line">            fmt.Println(a)</span><br><span class="line">        &#125;</span><br><span class="line">	    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        1:addr of arr: 0xc000012168, value of arr: [89 90 91]</span></span><br><span class="line"><span class="comment">        2:addr of arr: 0xc000006030, value of arr: &amp;[89 90 91]</span></span><br><span class="line"><span class="comment">        3:addr of arr: 0xc000006030, value of arr: [90 90 91]</span></span><br><span class="line"><span class="comment">        4:addr of arr: 0xc000012168, value of arr: [90 90 91]</span></span><br><span class="line"><span class="comment">	    */</span></span><br><span class="line">	</span><br><span class="line">	<span class="number">5.4</span> 切片传参</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">modify</span><span class="params">(sls []<span class="keyword">int</span>)</span></span> &#123;</span><br><span class="line">            sls[<span class="number">2</span>] = <span class="number">90</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            a := [<span class="number">3</span>]<span class="keyword">int</span>&#123;<span class="number">89</span>, <span class="number">90</span>, <span class="number">91</span>&#125;</span><br><span class="line">            modify(a[:])</span><br><span class="line">            fmt.Println(a) <span class="comment">// [89 90 90]</span></span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<h2 id="二、make"><a href="#二、make" class="headerlink" title="二、make"></a>二、make</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">make也是用于内存分配的，区别于new</span></span><br><span class="line"><span class="comment">它只用于slice、map以及chan的内存创建，而且它返回的类型就是这三个类型本身，而不是他们的指针类型</span></span><br><span class="line"><span class="comment">因为这三种类型就是引用类型，所以就没有必要返回他们的指针了。make函数的函数签名如下</span></span><br><span class="line"><span class="comment">	func make(t Type, size ...IntegerType) Type</span></span><br><span class="line"><span class="comment">make函数是无可替代的，我们在使用slice、map以及channel的时候，都需要使用make进行初始化，然后才可以对它们进行操作。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">需要像下面的示例代码一样使用make函数进行初始化操作之后，才能对其进行键值对赋值：</span></span><br><span class="line"><span class="comment">*/</span> </span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> b <span class="keyword">map</span>[<span class="keyword">string</span>]<span class="keyword">int</span></span><br><span class="line">	b = <span class="built_in">make</span>(<span class="keyword">map</span>[<span class="keyword">string</span>]<span class="keyword">int</span>, <span class="number">10</span>)</span><br><span class="line">	b[<span class="string">&quot;Ame&quot;</span>] = <span class="number">100</span></span><br><span class="line">	fmt.Println(b)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="三、new"><a href="#三、new" class="headerlink" title="三、new"></a>三、new</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">new是一个内置的函数，它的函数如下：</span></span><br><span class="line"><span class="comment">	func new(Type) *Type</span></span><br><span class="line"><span class="comment">其中:</span></span><br><span class="line"><span class="comment">    Type表示类型，new函数只接受一个参数，这个参数是一个类型</span></span><br><span class="line"><span class="comment">    *Type表示类型指针，new函数返回一个指向该类型内存地址的指针。</span></span><br><span class="line"><span class="comment">new函数不太常用，使用new函数得到的是一个类型的指针，并且该指针对应的值为该类型的零值。举个例子：</span></span><br><span class="line"><span class="comment">    func main() &#123;</span></span><br><span class="line"><span class="comment">        a := new(int)</span></span><br><span class="line"><span class="comment">        b := new(bool)</span></span><br><span class="line"><span class="comment">        fmt.Printf(&quot;%T\n&quot;, a) // *int</span></span><br><span class="line"><span class="comment">        fmt.Printf(&quot;%T\n&quot;, b) // *bool</span></span><br><span class="line"><span class="comment">        fmt.Println(*a)       // 0</span></span><br><span class="line"><span class="comment">        fmt.Println(*b)       // false</span></span><br><span class="line"><span class="comment">    &#125;	</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 指针作为引用类型需要初始化后才会拥有内存空间，才可以给它赋值。</span></span><br><span class="line"><span class="comment">// 使用内置的new函数对a进行初始化之后就可以正常对其赋值了：</span></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> a *<span class="keyword">int</span></span><br><span class="line">        a = <span class="built_in">new</span>(<span class="keyword">int</span>)</span><br><span class="line">        *a = <span class="number">10</span></span><br><span class="line">        fmt.Println(*a)</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>



<h2 id="四、make与new的区别"><a href="#四、make与new的区别" class="headerlink" title="四、make与new的区别"></a>四、make与new的区别</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">二者都是用来做内存分配的。</span></span><br><span class="line"><span class="comment">make只用于slice、map以及channel的初始化，返回的还是这三个引用类型本身；</span></span><br><span class="line"><span class="comment">而new用于类型的内存分配，并且内存对应的值为类型零值，返回的是指向类型的指针。</span></span><br><span class="line"><span class="comment">new并不会初始化，如果new了以后，还需要make为其初始化</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> <span class="built_in">new</span>的简单使用</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> i *<span class="keyword">int</span> = <span class="built_in">new</span>(<span class="keyword">int</span>)</span><br><span class="line">        *i = <span class="number">1000</span></span><br><span class="line">        fmt.Printf(<span class="string">&quot;p: %v, addr of i: %p&quot;</span>, *i, &amp;i)</span><br><span class="line">    &#125; <span class="comment">// p: 1000, addr of i: 0xc000006028</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> <span class="built_in">new</span>配合结构体使用</span><br><span class="line">    <span class="keyword">type</span> User <span class="keyword">struct</span> &#123;</span><br><span class="line">        Name <span class="keyword">string</span></span><br><span class="line">        Age  <span class="keyword">int</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> pUser *User = <span class="built_in">new</span>(User)</span><br><span class="line">        pUser.Name = <span class="string">&quot;Ame&quot;</span> <span class="comment">// (*pUser).Name = &quot;Ame&quot; 最本质的写法，但是go帮你优化了</span></span><br><span class="line">        pUser.Age = <span class="number">18</span></span><br><span class="line">        fmt.Println(pUser) <span class="comment">// &amp;&#123;Ame 18&#125;</span></span><br><span class="line">        fmt.Println(pUser.Name) <span class="comment">// Ame</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> <span class="built_in">new</span>和<span class="built_in">make</span>配合使用</span><br><span class="line">	<span class="number">3.1</span> 切片</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> p *[]<span class="keyword">int</span> = <span class="built_in">new</span>([]<span class="keyword">int</span>)</span><br><span class="line">            *p = <span class="built_in">make</span>([]<span class="keyword">int</span>, <span class="number">10</span>)</span><br><span class="line">            (*p)[<span class="number">0</span>] = <span class="number">10</span></span><br><span class="line">            (*p)[<span class="number">2</span>] = <span class="number">10</span></span><br><span class="line">            fmt.Print(*p) <span class="comment">// [10 0 10 0 0 0 0 0 0 0]</span></span><br><span class="line">        &#125;</span><br><span class="line">	<span class="number">3.2</span> <span class="keyword">map</span></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> p *<span class="keyword">map</span>[<span class="keyword">string</span>]<span class="keyword">int</span> = <span class="built_in">new</span>(<span class="keyword">map</span>[<span class="keyword">string</span>]<span class="keyword">int</span>)</span><br><span class="line">            *p = <span class="built_in">make</span>(<span class="keyword">map</span>[<span class="keyword">string</span>]<span class="keyword">int</span>, <span class="number">10</span>)</span><br><span class="line">            (*p)[<span class="string">&quot;key&quot;</span>] = <span class="number">100</span></span><br><span class="line">            (*p)[<span class="string">&quot;key2&quot;</span>] = <span class="number">200</span></span><br><span class="line">            fmt.Print(*p)</span><br><span class="line">        &#125; <span class="comment">// map[key:100 key2:200]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>


            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2020/03/04/Go%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B------%E6%8C%87%E9%92%88/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2020/03/04/Go%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B------%E6%8C%87%E9%92%88/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
